const categoryService = require("../services/categoryService");

class CategoryController {
  // 创建分类
  async create (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await categoryService.create(data, user_id);

    ctx.body = {
      code: 200,
      data: null,
      message: '创建成功'
    }
  }

  // 更新分类
  async update (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    const category = await categoryService.update(data, user_id);
    ctx.body = {
      code: 200,
      data: category,
      message: '更新成功'
    }
  }

  // 删除分类
  async delete (ctx) {
    const { id } = ctx.query;
    await categoryService.delete(id);
    ctx.body = {
      code: 200,
      data: null,
      message: '删除成功'
    }
  }

  // 获取单个分类
  async findOne (ctx) {
    const { id } = ctx.query;
    const category = await categoryService.findOne(id);
    ctx.body = {
      code: 200,
      data: category,
      message: '查询成功'
    }
  }

  // 获取所有分类
  async findAll (ctx) {
    const { type } = ctx.query;
    const categories = await categoryService.findAll(type);
    ctx.body = {
      code: 200,
      data: categories,
      message: 'Success'
    }
  }
}

module.exports = new CategoryController();